package genomemap.util;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 
 * @param <T> 
 * @author Susanta Tewari
 */
public class FreqDist<T> {

    private Map<T, Integer> freqDist = new Hashtable<T, Integer>();

    public FreqDist() {
    }

    public FreqDist(Collection<T> list) {

        add(list);

    }

    public Map<T, Integer> getFreqDist() {
        return freqDist;
    }

    public Map<Integer,Set<T>> getInverseFreqDist() {

        Map<Integer,Set<T>> inverseDist = new HashMap<Integer,Set<T>>();

        for(T t : freqDist.keySet()){

            Integer freq = freqDist.get(t);

            if (inverseDist.get(freq) == null) inverseDist.put(freq, new HashSet<T>());

            inverseDist.get(freq).add(t);
            
        }

        return inverseDist;
    }

    public void add(Collection<T> list) {

        for (T t : new HashSet<T>(list)) {

            int freq = 0;

            for (T val : list) {
                if (val.equals(t)) {
                    freq++;
                }
            }

            if (freqDist.get(t) != null) {
                int oldVal = freqDist.get(t).intValue();
                freqDist.remove(t);
                freqDist.put(t, oldVal+freq);
            } else {
                freqDist.put(t, freq);
            }

        }

    }


    

}
